home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: How to get a random s
- Date: 27 Feb 1996 11:19:52 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4gupco$q9r@gail.ripco.com>
- NNTP-Posting-Host: foley.ripco.com
-
-
- chancl@nevada.edu (Clapton Chan) asked, among other things, about
- seeding rand.
-
- After posting a correct and legal answer (snipped away by Alan Bowler),
- I ended that article with
-
- > [A poor practice follows]
- > if you do not #include <time.h>, you need
- > srand((unsigned)time(NULL));
-
- Mr. Bowler, who apparently did not understand `a poor practice' as
- strong condemnation, inserted the hyperbole
-
- > Worse than poor.
-
- It seems that Mr. Bowler condones poor programming practice and condemns
- only the `worse than poor'. [Insert your own damn smilies.] Poor
- practices are to avoided; this one is unfortunately very common.
-
- In any case, Mr. Bowler pointed out
- > Adding the cast to unsigned will not change that fact that
- > this implicit declaration means your code could be looking in the
- > wrong place.
-
- The `wrong place' argument -- and discussion about registers for
- different machines -- is not needed for to condemn this code.
-
- gumboot@airdmhor.gen.nz (Simon Hosie) in <4gqir9$d5r@airdmhor.gen.nz>
- now asks:
-
- > I don't follow that.. if time returns a float then won't the result be
- >cast and truncated to an int?
-
- The problem is that, without including a declaration for time(), which
- is the point of the `#include <time.h>', the compiler has no way of
- knowing that time() returns anything other than an int.
-
- In fact, it will assume that time() _does_ return an int. It will not
- cast it from whatever a time_t is to an integer type. The explicit cast
- converts the presumed int return value (even if a floating type was
- returned) to an unsigned int. This is not very helpful when time_t is a
- floating point type.
-
- Even if the same registers are used for time_t and int values, if time_t
- is not an int, then the value time() returns is not the value that the
- caller gets. The same bit pattern will be interpreted differently.
-
- Mr. Bowler is taking a different tack on this. His point is that time()
- may return its value in a different register (e.g. floating point reg)
- from that the caller expects (an integer reg). In that case, the value
- time() returns will not never even be seen by the caller.
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-